<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\CarUpkeepDao;
use app\model\car\CarInfo;
use app\model\car\CarUpkeep;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;
use think\exception\ValidateException;
use think\facade\Db;

class CarUpkeepServices extends BaseServices
{
    /**
     * CarUpkeepServices constructor.
     * @param CarUpkeepDao $dao
     */
    public function __construct(CarUpkeepDao $dao)
    {
        return $this->dao = $dao;
    }

    /**
     * 提交保养表单
     * @param array $data
     * @param array $op_user
     * @param int $op_user_type
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function submitUpkeepData(array $data,array $op_user,int $op_user_type){
        $carInfo = Db::name('car_info')->where(['id'=>$data['car_id']])->find();
        if(!$carInfo) throw new ValidateException('未查询到车辆信息');
        if($carInfo['run_km'] > $data['upkeep_km']) throw new ValidateException('输入错误，请认真核对后填写');
        $data['upkeep_date'] = strtotime($data['upkeep_date']);
        $data['add_time'] = time();
        $res = CarUpkeep::create($data);
        if(!$res) throw new ValidateException('提交失败');
        $res = CarInfo::where(['id'=>$data['car_id']])->update(['last_upkeep_date'=>$data['upkeep_date'],'last_upkeep_km'=>$data['upkeep_km']]);
        if(!$res) throw new ValidateException('表单回写失败');
        $title = '店员'.$op_user['user_name'].'提交了车辆保养表单，提交值里程【'.$data['upkeep_km'].'】,保养时间【'.date('Y-m-d',$data['upkeep_date']).'】';
        BaseLogServices::addLog($data['car_id'],2,$title,$op_user_type,$op_user['id'],$op_user['user_name'],20);
        return '提交成功';
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [],bool $is_page = false){
        $page = $limit = 0;
        if($is_page){
            [$page,$limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$page,$limit);
        foreach ($list['list'] as &$item){
            $item['upkeep_date'] = $item['upkeep_date'] ? date('Y-m-d',$item['upkeep_date']) : '未知';
        }
        return $list;
    }

    /**
     * 删除
     * @param int $id
     * @return string
     */
    public function delete(int $id){
        $res = $this->dao->update($id,['is_del'=>1,'delete_time'=>time()]);
        if(!$res) throw new AdminException('删除失败,请稍候再试!');
        return '删除成功!';
    }
}
